package com.huiyou.adminapi.controller;

import com.huiyou.adminapi.common.model.Goods;
import com.huiyou.adminapi.service.GoodsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/goods")
@RestController
public class GoodsController extends AbstractController {

    private final GoodsService goodsService;

    @Autowired
    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @GetMapping
    public Page<Goods> findAll(String name, @PageableDefault Pageable pageable) {
        return this.goodsService.findAll(name, pageable);
    }

    @GetMapping("/{id}")
    public Goods findById(@PathVariable("id") String id) {
        return this.goodsService.findById(id);
    }

    @PostMapping
    public void save(@RequestBody Goods goods) {
        this.goodsService.save(goods);
    }

    @PutMapping("/{id}")
    public void update(@PathVariable("id") String id, @RequestBody Goods goods) {
        super.idCheckFound(goodsService.findById(id));
        this.goodsService.save(goods);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") String id) {
        this.goodsService.deleteById(id);
    }


    @GetMapping("/findProId")
    public List<Goods> findByProId(@RequestParam String proId){
        return this.goodsService.findByProId(proId);
    }
}
